<template>
  <div class="login_container">
    <el-row>
      <el-col :span="12" :xs="0"></el-col>
      <el-col :span="12" :xs="24">
        <el-form class="login_form">
          <h1>Hello</h1>
          <h2>welcome to shopping!</h2>
          <el-form-item>
            <el-input placeholder="请输入用户名" :prefix-icon="User" v-model="loginForm.username"></el-input>
          </el-form-item>
          <el-form-item>
            <el-input placeholder="请输入密码" type="password" :prefix-icon="Lock" show-password v-model="loginForm.password"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button class="login_btn" type="primary" :loading="loading" @click="login">登录</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue';
import { User, Lock } from '@element-plus/icons-vue';
import { ElNotification } from 'element-plus'

// 引入用户仓库
import useUserStore from '@/store/modules/user';
let userStore = useUserStore();

import { useRouter } from 'vue-router';
let $router = useRouter()

const loginForm = reactive({ username: 'admin', password: '111111'});

// 登录按钮的加载状态
let loading = ref(false);
// 登录
const login = async () => {
  loading.value = true;

  try {
   let res = await userStore.userLogin(loginForm)
    
    $router.push('/')
    ElNotification({
      type: 'success',
      message: '登录成功'
    })

    loading.value = false;
  } catch (error){
    ElNotification({
      type: 'error',
      message: (error as Error).message
    })

    loading.value = false;
  }
}
</script>

<style scoped>
.login_container {
  width: 100%;
  height:100vh;
  background: url('@/assets/images/background.jpg') no-repeat;
  background-size: cover;
}
.login_form{
  position: relative;
  width: 80%;
  top: 30vh;
  background: url('@/assets/images/login_form.png') no-repeat;
  background-size: cover;
  padding: 40px;
  h1{
    font-size: 40px;
    color: #fff;
  }
  h2{
    font-size: 20px;
    color: #fff;
    margin: 20px 0;
  }
  .login_btn{
    width: 100%;
  }
}
</style>